﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace ESMAM.WebApp
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            var app = (MvcApplication)sender;
            var context = app.Context;
            var ex = app.Server.GetLastError();

            //记录错误日志
            LogUtils.LogError("网站出错", Developer.MJ, ex, ex.InnerException);

            context.Response.Clear();
            context.ClearError();

            var httpException = ex as HttpException;
            var routeData = new RouteData();
            routeData.Values["controller"] = "error";
            routeData.Values["exception"] = ex;
            routeData.Values["action"] = "http500";
            if (httpException != null)
            {
                switch (httpException.GetHttpCode())
                {
                    case 404:
                        routeData.Values["action"] = "http404";
                        break;
                    case 401:  //没有登录

                        routeData.Values["action"] = "http401";
                        break;
                    case 400:  //演示版本，没有执行的权限

                        routeData.Values["action"] = "DemoError";
                        break;
                }
            }
            //IController controller = new ErrorController();
            //controller.Execute(new RequestContext(new HttpContextWrapper(context), routeData));
        }
    }
}
